//
//  SXEditorViewController.m
//  SXDataManager_Example
//
//  Created by wangshuixin on 2019/1/30.
//  Copyright © 2019 wangshuixin. All rights reserved.
//

#import "SXEditorViewController.h"
#import <WebKit/WebKit.h>

@interface SXEditorViewController ()<WKNavigationDelegate,WKUIDelegate>
@property (strong, nonatomic) WKWebView *webView;

@end

@implementation SXEditorViewController

#pragma mark - Lazy load
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
- (WKWebView *)webView {
    if (!_webView) {
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height - 100)];
        _webView.navigationDelegate = self;
        _webView.UIDelegate = self;
        NSBundle *bundlePath = [NSBundle performSelector:@selector(SXDataManager_bundle) withObject:nil];
        NSString *htmlPath = [bundlePath pathForResource:@"SX_attributeTextEditor" ofType:@"html"];
        NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
        NSString *htmlContent = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
        [_webView loadHTMLString:htmlContent baseURL:baseURL];
        // 开了支持滑动返回
        _webView.allowsBackForwardNavigationGestures = NO;
        // 禁止webview的弹跳
        _webView.scrollView.bounces = NO;
        _webView.inputAccessoryView.hidden = YES;
    }
    return _webView;
}
#pragma clang diagnostic pop

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.webView];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 60, 30);
    leftButton.backgroundColor = [UIColor redColor];
    [leftButton addTarget:self action:@selector(getHtmlString) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.rightBarButtonItems = @[leftItem];
}
- (void)getHtmlString {
//    [_webView evaluateJavaScript:@"var hm = document.createElement(\"script\");hm.src = \"123\";var s = document.getElementsByTagName(\"body\")[0];s.parentNode.insertBefore(hm, s);" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
//        
//    }];
    [_webView evaluateJavaScript:@"var target=document.getElementById(\"back-text\");target.style.display=\"none\";" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
        
    }];
    [_webView evaluateJavaScript:@"document.getElementsByTagName('html')[0].innerHTML;" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
